﻿Imports DotNetNuke.Services.Journal

Namespace DotNetNuke.Modules.ActiveForums
#Region "ReplyInfo"
    Public Class ReplyInfo
#Region "Private Members"
        Private _ReplyId As Integer
        Private _TopicId As Integer
        Private _ReplyToId As Integer
        Private _ContentId As Integer
        Private _StatusId As Integer
        Private _IsApproved As Boolean
        Private _IsDeleted As Boolean
        Private _Content As Content
        Private _Author As Author
#End Region
#Region "Public Properties"
        Public Property ReplyId() As Integer
            Get
                Return _ReplyId
            End Get
            Set(ByVal value As Integer)
                _ReplyId = value
            End Set
        End Property
        Public Property ReplyToId() As Integer
            Get
                Return _ReplyToId
            End Get
            Set(ByVal value As Integer)
                _ReplyToId = value
            End Set
        End Property
        Public Property TopicId() As Integer
            Get
                Return _TopicId
            End Get
            Set(ByVal value As Integer)
                _TopicId = value
            End Set
        End Property
        Public Property ContentId() As Integer
            Get
                Return _ContentId
            End Get
            Set(ByVal value As Integer)
                _ContentId = value
            End Set
        End Property
        Public Property StatusId() As Integer
            Get
                Return _StatusId
            End Get
            Set(ByVal value As Integer)
                _StatusId = value
            End Set
        End Property
        Public Property IsApproved() As Boolean
            Get
                Return _IsApproved
            End Get
            Set(ByVal value As Boolean)
                _IsApproved = value
            End Set
        End Property
        Public Property IsDeleted() As Boolean
            Get
                Return _IsDeleted
            End Get
            Set(ByVal value As Boolean)
                _IsDeleted = value
            End Set
        End Property
        Public Property Content() As Content
            Get
                Return _Content
            End Get
            Set(ByVal value As Content)
                _Content = value
            End Set
        End Property
        Public Property Author() As Author
            Get
                Return _Author
            End Get
            Set(ByVal value As Author)
                _Author = value
            End Set
        End Property
#End Region
        Public Sub New()
            Content = New Content
            Author = New Author
        End Sub
    End Class
#End Region
#Region "Reply Controller"
    Public Class ReplyController
#Region "Public Methods"
        Public Sub Reply_Delete(PortalId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal ReplyId As Integer, ByVal DelBehavior As Integer)
            DataProvider.Instance.Reply_Delete(ForumId, TopicId, ReplyId, DelBehavior)
            Dim objectKey As String = String.Format("{0}:{1}:{2}", ForumId.ToString(), TopicId.ToString(), ReplyId.ToString())
            JournalController.Instance.DeleteJournalItemByKey(PortalId, objectKey)
        End Sub
        Public Function Reply_QuickCreate(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal ReplyToId As Integer, ByVal Subject As String, ByVal Body As String, ByVal UserId As Integer, ByVal DisplayName As String, ByVal IsApproved As Boolean, ByVal IPAddress As String) As Integer
            Dim replyId As Integer = -1
            Dim ri As New ReplyInfo
            With ri
                Dim dt As Date = Now()
                .Content.DateUpdated = dt
                .Content.DateCreated = dt
                .Content.AuthorId = UserId
                .Content.AuthorName = DisplayName
                .Content.Subject = Subject
                .Content.Body = Body
                .Content.IPAddress = IPAddress
                .Content.Summary = String.Empty
                .IsApproved = IsApproved
                .IsDeleted = False
                .ReplyToId = ReplyToId
                .StatusId = -1
                .TopicId = TopicId
            End With
            replyId = Reply_Save(PortalId, ri)
            Return replyId
        End Function
        Public Function Reply_Save(ByVal PortalId As Integer, ByVal ri As ReplyInfo) As Integer
            Return CType(DataProvider.Instance().Reply_Save(PortalId, ri.TopicId, ri.ReplyId, ri.ReplyToId, ri.StatusId, ri.IsApproved, ri.IsDeleted, ri.Content.Subject.Trim, ri.Content.Body.Trim, ri.Content.DateCreated, ri.Content.DateUpdated, ri.Content.AuthorId, ri.Content.AuthorName, ri.Content.IPAddress), Integer)
        End Function
        Public Function Reply_Get(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal TopicId As Integer, ByVal ReplyId As Integer) As ReplyInfo
            Dim dr As IDataReader = DataProvider.Instance().Reply_Get(PortalId, ModuleId, TopicId, ReplyId)
            Dim ri As ReplyInfo = Nothing
            While dr.Read()
                ri = New ReplyInfo
                With ri
                    .ReplyId = CInt(dr("ReplyId"))
                    .ReplyToId = CInt(dr("ReplyToId"))
                    .Content.AuthorId = CInt(dr("AuthorId"))
                    .Content.AuthorName = dr("AuthorName").ToString
                    .Content.Body = dr("Body").ToString
                    .Content.ContentId = CInt(dr("ContentId"))
                    .Content.DateCreated = CDate(dr("DateCreated"))
                    .Content.DateUpdated = CDate(dr("DateUpdated"))
                    .Content.IsDeleted = CBool(dr("IsDeleted"))
                    .Content.Subject = dr("Subject").ToString
                    .Content.Summary = dr("Summary").ToString
                    .Content.IPAddress = dr("IPAddress").ToString
                    .Author.AuthorId = .Content.AuthorId
                    .Author.DisplayName = dr("DisplayName").ToString
                    .Author.Email = dr("Email").ToString
                    .Author.FirstName = dr("FirstName").ToString
                    .Author.LastName = dr("LastName").ToString
                    .Author.Username = dr("Username").ToString
                    .ContentId = CInt(dr("ContentId"))
                    .IsApproved = CBool(dr("IsApproved"))
                    .IsDeleted = CBool(dr("IsDeleted"))
                    .StatusId = CInt(dr("StatusId"))
                    .TopicId = CInt(dr("TopicId"))
                End With
                'tl.Add(ti)
            End While
            dr.Close()
            Return ri
        End Function
        Public Function ApproveReply(PortalId As Integer, TabId As Integer, ModuleId As Integer, ForumId As Integer, TopicId As Integer, ReplyId As Integer) As ReplyInfo
            Dim ms As SettingsInfo = DataCache.MainSettings(ModuleId)
            Dim fc As New ForumController
            Dim fi As Forum = fc.Forums_Get(ForumId, -1, False, True)

            Dim rc As New ReplyController
            Dim reply As ReplyInfo = rc.Reply_Get(PortalId, ModuleId, TopicId, ReplyId)
            If reply Is Nothing Then
                Return Nothing
            End If
            reply.IsApproved = True
            rc.Reply_Save(PortalId, reply)
            Dim tc As New TopicsController
            tc.Topics_SaveToForum(ForumId, TopicId, PortalId, ModuleId, ReplyId)
            Dim topic As TopicInfo = tc.Topics_Get(PortalId, ModuleId, TopicId, ForumId, -1, False)

            If fi.ModApproveTemplateId > 0 And reply.Author.AuthorId > 0 Then
                Dim oEmail As New Email
                oEmail.SendEmail(fi.ModApproveTemplateId, PortalId, ModuleId, TabId, ForumId, TopicId, ReplyId, String.Empty, reply.Author)
            End If

            Subscriptions.SendSubscriptions(PortalId, ModuleId, TabId, ForumId, TopicId, ReplyId, reply.Content.AuthorId)

            Try
                Dim ctlUtils As New ControlUtils
                Dim fullURL As String = ctlUtils.BuildUrl(TabId, ModuleId, fi.ForumGroup.PrefixURL, fi.PrefixURL, fi.ForumGroupId, ForumId, TopicId, topic.TopicUrl, -1, -1, String.Empty, 1, fi.SocialGroupId)

                If fullURL.Contains("~/") Then
                    fullURL = Utilities.NavigateUrl(TabId, "", New String() {ParamKeys.TopicId & "=" & TopicId, ParamKeys.ContentJumpId & "=" & ReplyId})
                End If
                If fullURL.EndsWith("/") Then
                    fullURL &= "?" & ParamKeys.ContentJumpId & "=" & ReplyId
                End If
                Dim amas As New Social
                amas.AddReplyToJournal(PortalId, ModuleId, ForumId, TopicId, ReplyId, reply.Author.AuthorId, fullURL, reply.Content.Subject, String.Empty, reply.Content.Body, fi.ActiveSocialSecurityOption, fi.Security.Read, fi.SocialGroupId)
            Catch ex As Exception
                DotNetNuke.Services.Exceptions.LogException(ex)
            End Try
            Return reply
        End Function
#End Region
    End Class
#End Region

End Namespace

